package com.wang.controller;


import com.aaa.entity.OCart;

import com.aaa.entity.UUserInfo;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;

import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wang.mapper.OcartDao;
import com.wang.mapper.UUserInfoMapper;
import com.wang.service.GGoodsService;
import com.wang.service.MyCartVoService;
import com.wang.service.OOrderService;
import com.wang.vo.GoodsVo1;
import com.wang.vo.MyCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author :  DongZheng Wang
 * @PROJECT_NAME: fifth-cai-parent
 * @date :  2023-03-06 16:10
 **/

@RestController
@RequestMapping("/order/cart")
public class MyCartController {
    @Autowired
    private MyCartVoService myCartVoService;
    @Autowired
    private GGoodsService gGoodsService;
    @Autowired
    private UUserInfoMapper uUserInfoMapper;
    @Autowired
    private OcartDao ocartDao;
    @Autowired
    private OOrderService oOrderService;

    @RequestMapping("/findCart")
    public Result listCart(Integer currentPage, Integer pageSize) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        Result lists = myCartVoService.lists(username, (currentPage - 1) * pageSize, pageSize);
        return new Result(2000, "success", lists);
    }

    /**
     * 单独删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteCart")
    public Result delete(Integer id) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        QueryWrapper<UUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone", username);
        UUserInfo uUserInfo = uUserInfoMapper.selectOne(wrapper);
        uUserInfo.getId();
        QueryWrapper<OCart> oCartQueryWrapper = new QueryWrapper<>();
        oCartQueryWrapper.eq("uid", uUserInfo.getId());
        oCartQueryWrapper.eq("goods_id", id);
        int delete = ocartDao.delete(oCartQueryWrapper);
        if (delete != 0) {
            return new Result(2000, "success");
        }
        return new Result(5000, "error");
    }

    /**
     * 批量删除
     */
    @RequestMapping("/deleteByIds")
    public Result delete(@RequestBody List<String> ids) {
        System.out.println(ids);
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        QueryWrapper<UUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone", username);
        UUserInfo uUserInfo = uUserInfoMapper.selectOne(wrapper);
        for (String id : ids) {
            QueryWrapper<OCart> oCartQueryWrapper = new QueryWrapper<>();
            oCartQueryWrapper.eq("uid", uUserInfo.getId());
            oCartQueryWrapper.eq("goods_id", id);
            int delete = ocartDao.delete(oCartQueryWrapper);
        }


        gGoodsService.removeByIds(Arrays.asList(ids));
        return new Result(2000, "success");
    }

    /***
     * 购物车进行
     * 添加购物
     */

    @RequestMapping("/settlement")
    public Result getGoods(@RequestBody List<GoodsVo1> oOrders1) {
        Result result = oOrderService.pageList(oOrders1);
        return new Result(2000,"sucess",result);
    }
    @RequestMapping("/verify")
    public Result verify(@RequestBody List<GoodsVo1> oOrders1){
        return oOrderService.findByRepertory(oOrders1);
    }


}
